<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        let detectLength = str => {
            if (str.length != 25) {
                console.log("字符串的长度不对")
                return -1;
            } else {
                return 1;
            }
        }

        let strToArr = str => {
            var len = 5;
            var newArr = [];
            var reg = new RegExp(".{" + len + "}", "g");
            newArr = str.match(reg);
            var newArr = newArr.map(element => {
                return element.split("");
            });
            show(newArr);
            return newArr;
        }

        let show = arr => {
            var str = "";
            for (var i = 0; i != arr.length; i++) {
                for (var j = 0; j != arr.length; j++) {
                    str += arr[i][j] + " ";
                }
                console.log(str);
                str = "";
            }
            console.log("转换");
        }

        let findBegin = arr => {
            var [xpos, ypos] = [0, 0];
            for (var i = 0; i != arr.length; i++) {
                for (var j = 0; j != arr.length; j++) {
                    if (arr[i][j] == '0') {
                        [xpos, ypos] = [i, j];
                    }
                }
            }
            return [xpos, ypos];
        }

        let run = (dir, xpos, ypos) => {
            switch (dir) {
                case 'A':
                case 'a':
                    if (xpos - 1 < 0) {
                        return 0;
                    } else {
                        xpos = xpos - 1;
                    }
                    break;
                case 'B':
                case 'b':
                    if (xpos + 1 > 4) {
                        return 0;
                    } else {
                        xpos = xpos + 1;
                    }
                    break;
                case 'L':
                case 'l':
                    if (ypos - 1 < 0) {
                        return 0;
                    } else {
                        ypos = ypos - 1;
                    }
                    break;
                case 'R':
                case 'r':
                    if (ypos + 1 > 4) {
                        return 0;
                    } else {
                        ypos = ypos + 1;
                    }
                    break;
                default:
                    console.log("end");
                    return 0;
            }
            return [xpos, ypos];
        }

        let reverse = (arr, oldxpos, oldypos, xpos, ypos) => {
            [arr[oldxpos][oldypos], arr[xpos][ypos]] = [arr[xpos][ypos], arr[oldxpos][oldypos]];
        }

        let main = (str, dir) => {
            var xpos, ypos, oldxpos, oldypos;
            if (detectLength(str)) {
                var arr = strToArr(str);
                [xpos, ypos] = findBegin(arr);
                if (dir.length == 0) {
                    console.log("wrong");
                    return -1;
                } else {
                    for (var i = 0; i != dir.length ;i ++) {
                        [oldxpos, oldypos] = [xpos, ypos];
                        try {
                            [xpos, ypos] = run(dir[i], xpos, ypos);
                            reverse(arr, oldxpos, oldypos, xpos, ypos);
                            show(arr);
                        }catch(e) {
                            console.log("越界");
                        }
                        
                        
                    }
                }
            }
        }

        main('ABCDEFG0HIJKLMNOPQRSTUVWX','ablraaaa');
    </script>
</body>

</html>